<?php
/**
 * Created by PhpStorm.
 * User: kcross
 * Date: 20-6-26
 * Time: 下午3:57
 */
namespace App\Lib\Sdk\Spider;

use App\Lib\Sdk\Spider\Service\Spider;

class JdSpider extends BaseSpider implements Spider
{
    public function search(array $jdConfig, $itemId = null)
    {
        $jdConfig = [
            'goodsReqDTO' => json_encode([
                'keyword' => '显示器'
            ])
        ];
        $api = JdApi::search2;
        $params = array_merge($jdConfig, [
            'method' => $api
        ]);
        $params = $this->getSign($params);
        $res = $this->client->get("", $params);
        return $res;
    }

    public function getCouponLink($url, $coupon)
    {

    }

    /**
     * @param $params
     * @return arrayf980aee5fe677d5695c5c4a5f4532439    京东安卓secretkey：
    35d19c976bfc45c9835153d3a09665e9
     */
    public function getSign($params)
    {
        $params = array_merge($params, [
            'timestamp' => date("yyyy-MM-dd  HH:mm:ss"),
            'format' => 'json',
            'v' => '1.0',
            'sign_method' => 'md5',
            'app_key' => 'f980aee5fe677d5695c5c4a5f4532439'
        ]) ;

        $params = array_filter($params, function($item) {
            if ($item === null || $item === "") {
                return false;
            }
            return true;
        });
        ksort($params);
        $str = "";
        foreach ($params as $k => $v) {
            $str .= $k.$v;
        }
        $str =  "35d19c976bfc45c9835153d3a09665e9" . $str . "35d19c976bfc45c9835153d3a09665e9";
        $sign = strtoupper(hash("md5", $str));
        $params['sign'] = $sign;
        return $params;

    }
}